using System;
using Aurora.TaskScheduler.Abstractions.BaseModel;
using Aurora.TaskScheduler.Dashboard.Middleware;
using Microsoft.AspNetCore.Builder;

namespace Aurora.TaskScheduler.Dashboard;

public static class TaskSchedulerBuilderExtension
{
    /// <summary>
    /// 使用ui
    /// </summary>
    public static TaskSchedulerOptions AddQuartzUI(this TaskSchedulerOptions options,
        Action<DashboardOptions> configure)
    {
        if (configure == null) throw new ArgumentNullException(nameof(configure));
        options.RegisterExtension(new DashboardTaskSchedulerOptionExtension(configure));
        return options;
    }
    

    /// <summary>
    /// 注入授权信息
    /// </summary>
    /// <param name="builder"></param>
    /// <returns></returns>
    public static IApplicationBuilder UseQuartzUIBasicAuthorized(this IApplicationBuilder app)
    {
        return app.UseMiddleware<BasicAuthMiddleware>();
    }
    

    /// <summary>
    /// 使用ui
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IApplicationBuilder UseQuartzUI(this IApplicationBuilder app)
    {
        app.UseRouting();
        app.UseStaticFiles();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapControllers();
        });
        app.UseQuartzUIBasicAuthorized();
   
        return app;
    }
}